package cn.git.demo.service.impl;

import cn.git.demo.dao.ProductDao;
import cn.git.demo.pojo.Brand;
import cn.git.demo.pojo.Product;
import cn.git.demo.pojo.ProductQuery;
import cn.git.demo.service.ProductService;
import cn.itcast.common.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("productService")
@Transactional
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    public Pagination selectPaginationByQuery(String name,Integer isShow,Integer pageNo) {

        ProductQuery productQuery = new ProductQuery();

        productQuery.setPageNo(Pagination.cpn(pageNo));

        productQuery.setPageSize(8);


        StringBuffer str = new StringBuffer();

        if (name != null){
            productQuery.setName(name);
            str.append("name=").append(name);
        }
        if (isShow != null){
            productQuery.setIsShow(isShow);
            str.append("&isShow=").append(isShow);
        }

        Pagination pagination = new Pagination(
          productQuery.getPageNo(),
          productQuery.getPageSize(),
          productDao.selectTotalCount(productQuery)
        );

        pagination.setList(productDao.selectProductListByQuery(productQuery));

//        分页展示
        String url = "/product/list.do";

        pagination.pageView(url,str.toString());

        return pagination;
    }

    public Product selectProductById(Long id) {

        return productDao.selectProductById(id);

    }

    public void updateProductById(Product product) {

        productDao.updateProductById(product);
    }

    public List<Brand> selectBrandName() {
        //查询品牌名称
        return productDao.selectBrandName();
    }

    public List<Product> selectProductByIds(Long[] ids){

       return productDao.selectProductByIds(ids);
    }


}



